/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.provider;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.phonebooksharing.android.provider.PhonebookSharing.Dashboard;
import com.phonebooksharing.android.provider.PhonebookSharing.DashboardSync;
import com.phonebooksharing.android.provider.PhonebookSharing.Message;
import com.phonebooksharing.android.provider.PhonebookSharing.SendTo;
import com.phonebooksharing.android.provider.PhonebookSharing.Sync;
import com.phonebooksharing.android.util.CloseableUtil;

public class PhonebookSharingManager {

    public static synchronized Dashboard getDashboard(ContentResolver cr, Uri data) {
        Dashboard inbox = null;
        Cursor c = null;

        try {
            c = cr.query(data, null, null, null, null);
            if (c.getCount() > 0) {
                if (c.moveToFirst()) {
                    inbox = Dashboard.fromCursor(c);
                }
            } else {
                inbox = new Dashboard();
            }
        } finally {
            CloseableUtil.safeClose(c);
            c = null;
        }

        return inbox;
    }

    public static synchronized ArrayList<Sync> getSyncList(ContentResolver cr, String sql) {
        ArrayList<Sync> list = null;
        Cursor c = null;
        try {
            c = cr.query(Sync.CONTENT_URI, null, sql, null, Sync._ID + " DESC");
            if (c.getCount() > 0) {
                list = Sync.fromCursor(c);
            } else {
                list = new ArrayList<Sync>();
            }
        } finally {
            CloseableUtil.safeClose(c);
            c = null;
        }
        return list;
    }
    
    public static synchronized void updateSyncStatus(ContentResolver cr, Uri sync, int status) {
        if(sync != null) {
            final ContentValues cv = new ContentValues();
            cv.put(Sync.SYNC_STATUS, status);
            cr.update(sync, cv, null, null);
        }
    }
    
    /**
     * @param Context
     * @param DashboardItemID
     * @param Action
     */
    public static synchronized void addDashboardSync(Object...params) {
        final ContentResolver cr = ((Context)params[0]).getContentResolver();
        final ContentValues cv = new ContentValues();
        cv.put(DashboardSync.DASHBOARD_ITEM_ID, "" + params[1]);
        cv.put(DashboardSync.ACTION, "" + params[2]);
        
        cr.insert(DashboardSync.CONTENT_URI, cv);
    }
    
    /**
     * @param Context
     * @param Title
     * @param Text (either text itself or path to voice file)
     * @param Type (either Voice or Text)
     * @param Android contact ID
     * @param Dashboard Item ID
     */
    public static synchronized void addMsgSync(Object... params) {
        final ContentResolver cr = ((Context)params[0]).getContentResolver();
        ContentValues cv = new ContentValues();
        cv.put(Message.TITLE, "" + params[1]);
        cv.put(Message.TEXT, "" + params[2]);//local file
        cv.put(Message.TYPE, "" + params[3]);
        
        final Uri msgUri = cr.insert(Message.CONTENT_URI, cv);
        
        //add to whom this message is gonna be send to
        cv = new ContentValues();
        cv.put(SendTo.SENT_TO_GROUP, 0);
        cv.put(SendTo.CONTACT_ID, "" + params[4]);
        cv.put(SendTo.RELATE_TABLE, SendTo.RELATION_TO_MESSAGE);
        cv.put(SendTo.RELATE_ID, msgUri.getPathSegments().get(1));
        
        cr.insert(SendTo.CONTENT_URI, cv);

        //add sync to disapear from dashboard
        addDashboardSync(params[0], params[5], DashboardSync.ACTION_ARCHIVE);
    }
}
